const shop = require('../../../mixins/shop');
const api = require('../../../config/api');
const util = require('../../../utils/util');
const app = getApp()

const options = {
  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    cats: [{
      id: -1,
      name: "上新"
    }, {
      id: 0,
      name: "热销榜单"
    }],
    curIdx: -1,
    goodsList: [],
    page: 1,
    loading: true,
    loadmoreShow: false,
    isLastPage: false,
    isEmpty: false,
    postsGoods: [],
  },

  chooseTap(e) {
    let that = this;
    let postsGoods = wx.getStorageSync('postsGoods');
    let id = e.currentTarget.dataset.id;
    let idx = e.currentTarget.dataset.idx;
    let goodsList = that.data.goodsList;
    if (postsGoods.length > 8) {
      wx.showToast({
        title: "最多只能添加8件商品",
        icon: 'none'
      })
      return
    }
    for (let i in postsGoods) {
      if (postsGoods[i].id == id) {
        postsGoods.splice(i, 1)
        goodsList[idx].isSelected = false;
        that.setData({
          postsGoods: postsGoods,
          goodsList: goodsList
        })
        wx.setStorageSync('postsGoods', postsGoods);
        return
      }
    }

    let goods = {};
    goods.id = id;
    goods.name = e.currentTarget.dataset.name;
    goods.pic = e.currentTarget.dataset.pic;
    postsGoods.push(goods);
    wx.setStorageSync('postsGoods', postsGoods);
    goodsList[idx].isSelected = true;
    that.setData({
      postsGoods: postsGoods,
      goodsList: goodsList
    })
  },

  theSelected() {
    let that = this;
    let postsGoods = that.data.postsGoods;
    let goodsList = that.data.goodsList;
    for (let i of postsGoods) {
      for (let j of goodsList) {
        if (j.id == i.id) {
          j.isSelected = true;
        }
      }
    }
    that.setData({
      goodsList: goodsList
    })
  },

  onLoad: function (options) {
    let that = this;
    let postsGoods = wx.getStorageSync('postsGoods');
    if (!postsGoods) {
      wx.setStorageSync('postsGoods', []);
    } else {
      that.setData({
        postsGoods: postsGoods
      })
    }
    that.getShopClassify();
    that.getClassifyGoods();
  },

  delKeyword() {
    let that = this;
    that.setData({
      keyword: '',
      loading: true,
      loadmoreShow: false,
      isLastPage: false,
      isEmpty: false,
      page: 1,
      goodsList: [],
    });
    that.getClassifyGoods();
  },

  onSearchConfirm(e) {
    let that = this;
    that.setData({
      keyword: e.detail.value,
      loading: true,
      loadmoreShow: false,
      isLastPage: false,
      isEmpty: false,
      page: 1,
      goodsList: [],
    })
    that.getGoodsSearch();
  },

  switchRightTab: function (e) {
    let that = this;
    that.setData({
      curIdx: e.target.dataset.id,
      loading: true,
      loadmoreShow: false,
      isLastPage: false,
      isEmpty: false,
      page: 1,
      goodsList: [],
    });
    that.getClassifyGoods();
  },

  toGoodsDetail(e) {
    wx.navigateTo({
      url: '/pages/shop/goods-details/goods-details?id=' + e.currentTarget.dataset.id,
    })
  },

  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      loading: true,
      loadmoreShow: false,
      isLastPage: false,
      isEmpty: false,
      page: 1,
    });
    that.getClassifyGoods();
  },

  getClassifyGoods() {
    let that = this;
    util.request(api.getShopClassifyGoodsListUrl, {
      cid: that.data.curIdx,
      page: that.data.page,
    }).then(function (res) {
      let data = res.data;
      let args = {};
      args.loading = false;
      if (data.data.length > 0) {
        args.goodsList = that.data.goodsList.concat(data.data);
      } else if (data.data == "") {
        args.isLastPage = true;
        if (data.current_page == 1) {
          args.isEmpty = true;
        }
      }
      args.page = data.current_page;
      that.setData(args);
      that.theSelected();
    })
  },

  getGoodsSearch() {
    let that = this;
    util.request(api.getShopGoodsSearchUrl, {
      keyword: that.data.keyword,
      page: that.data.page,
    }).then(function (res) {
      let data = res.data;
      let args = {};
      args.loading = false;
      if (data.data.length > 0) {
        args.goodsList = that.data.goodsList.concat(data.data);
      } else if (data.data == "") {
        args.isLastPage = true;
        if (data.current_page == 1) {
          args.isEmpty = true;
        }
      }
      args.page = data.current_page;
      that.setData(args);
      that.theSelected();
    })
  },

  onReachBottom: function () {
    let that = this;
    that.setData({
      loadmoreShow: true,
      page: that.data.page + 1
    });
    if (that.data.keyword) {
      that.getGoodsSearch();
    } else {
      that.getClassifyGoods();
    }
  },

  onShareAppMessage: function (res) {},
}

shop(options)
Page(options)